// To parse this JSON data, do
//

import 'dart:convert';

import 'package:valovault_reminder/common/common_lib/val_data_base_manager.dart';
import 'package:valovault_reminder/common/common_lib/val_data_base_constant.dart';
import 'package:valovault_reminder/common/common_lib/val_data_base_entity.dart';
import 'package:valovault_reminder/common/util/val_app_state_tool.dart';


class ValPostRecordModel implements ValDataBaseEntityInterface {
  /// 注意： 表名
  static String dbName() => 'cs_user_post_table_${ValAppStateTool().accountInfo?.userId ?? ""}';
  /// 注意：主键
  static String primaryKey = 'id';
  /// 需要查询的key: 注意主键不需要
  static Map<String, ValDataBaseQueryEnum> queryKeys() {
    return {"userId": ValDataBaseQueryEnum.text,
      "category": ValDataBaseQueryEnum.text,
      "accountId": ValDataBaseQueryEnum.text,
    };
  }

  ValDataBaseEntity dbEntity = ValDataBaseManager.getEntity(
      dbTableName: ValPostRecordModel.dbName(),
      primaryKey: ValPostRecordModel.primaryKey,
      queryKeys: ValPostRecordModel.queryKeys());

  ValPostRecordModel({
    this.coverImage = '',
    this.image = '',
    this.postImage = '',
    this.userId = '99',
    this.id = 0,
    this.category = '',
    this.title = '',
    this.content = '',
    this.followed = '',
    this.publishDate = '',
    this.liked = false,
    this.collected = false,

    this.accountId = '1',
    this.accountImage = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    this.accountName = 'AeroFrisbee',
  });

  String coverImage;
  String image;
  String postImage; //发布第三张图
  String userId;
  int id;
  String category;
  String title;
  String content;
  String publishDate;
  String followed;
  bool liked;
  bool collected;

  String accountId; //绑定的作者
  String accountName; //绑定的作者名称
  String accountImage; //绑定的作者头像
  /// 记录元数据
  Map<String, dynamic>? _originData;

  ///写入数据库
  saveToDb() {
    _originData = toJson();
    dbEntity.save(this);
  }

  ///读取数据库
  Future<List<ValPostRecordModel>> readDb({String userId = ''}) async {
    /// 可以传查询条件
    List<VaDataBaseQuery> conditions = [];
    if (userId.isNotEmpty) {
      conditions = [VaDataBaseQuery('userId', userId)];
    }
    List<dynamic> postList = ( await dbEntity.get(query: conditions)) ?? [];

    List<ValPostRecordModel> list =
    (postList.map((e) => ValPostRecordModel.fromJson(e))
        .toList() ?? []).toList();
    return list;
  }

  ///读取数据库
  Future<List<ValPostRecordModel>> readCategoryDb(String category) async {
    /// 可以传查询条件
    List<VaDataBaseQuery> conditions = [VaDataBaseQuery('category', category)];
    List<dynamic> postList = ( await dbEntity.get(query: conditions)) ?? [];

    List<ValPostRecordModel> list =
    (postList.map((e) => ValPostRecordModel.fromJson(e))
        .toList() ?? []).toList();
    return list;
  }

  ///删除数据
  deleteDb(ValPostRecordModel postModel) {
    List<VaDataBaseQuery> conditions = [VaDataBaseQuery('id', postModel.id)];
    dbEntity.delete(ValPostRecordModel.dbName(), conditions);
  }

  ///删除数据
  deleteForUserId(String userId) {
    List<VaDataBaseQuery> conditions = [VaDataBaseQuery('accountId', userId)];
    dbEntity.delete(ValPostRecordModel.dbName(), conditions);
  }

  factory ValPostRecordModel.fromJson(Map<String, dynamic> json) {
    ValPostRecordModel postModel = ValPostRecordModel(
      coverImage: json["coverImage"],
      image: json["image"],
      userId: json["userId"],
      category: json["category"],
      id: json["id"],
      title: json["title"],
      content: json["content"],
      publishDate: json["publishDate"],
      followed: json["followed"],
      liked: json["liked"],
      postImage: json["postImage"],
      collected: json["collected"] ?? false,
      accountName: json["accountName"] ?? '',
      accountImage: json["accountImage"] ?? '',
      accountId: json["accountId"] ?? '',
    );
    postModel._originData = json;
    return postModel;
  }

  Map<String, dynamic> toJson() => {
    "coverImage": coverImage,
    "image": image,
    "userId": userId,
    "id": id,
    "category": category,
    "title": title,
    "content": content,
    "publishDate": publishDate,
    "followed": followed,
    "liked": liked,
    "postImage": postImage,
    "collected": collected,
    "accountId": accountId,
    "accountImage": accountImage,
    "accountName": accountName,
  };

  @override
  Map<String, dynamic> dbOriginData() {
    // TODO: implement dbOriginData
    return _originData ?? {};
  }

  @override
  Map<String, dynamic>? queryValues() {
    // TODO: implement queryValues
    return {
      "userId": userId,
      "accountId": accountId,
      "id": id,
      "category": category,
    };
  }
}
